//The estout package provides tools for making regression tables. 
//eststo stores a copy of the active estimation results for later tabulation. (It is an alternative to official Stata's estimates store.)
//esttab is a command to make publication-style tables.
//SW stands for stepwise, C stands for extra controls, and R stands for Robustness check.
use "C:\Users\wachbar1\WInequality\Analysis\Country\countryxyzcorr.dta"
cd "C:\Users\wachbar1\WInequality\Analysis\Country\Graphs_Tables"

label var meanUDS "UDS"
label var RGTQ "Social Trust"
label var MeanGiniSWIID "Gini Income"
label var MeanGDP "GDP"
label var GiniWealth "Gini Wealth"
label var prepr "Trust in Representative Institutions"
label var pimpl "Trust in Implementing Institutions"
label var z_meanUDS "UDS"
label var z_logRGTQ "Social Trust"
label var z_MeanGiniSWIID "Gini Income"
label var z_logMeanGDP "GDP"
label var z_GiniWealth "Gini Wealth"
label var z_prepr "Trust in Representative Institutions"
label var z_pimpl "Trust in Implementing Institutions"
label var t "Time Interval"
label var z_prepr_wg "Trust in Representative Institutions (excl. item on government)"

order ISO3166_2-logRGTQ GTQmis preprimplmis prepr_wg GTQmis preprimplmis ///
z_GiniWealth-z_meanUDS z_logMeanGDP z_logRGTQ z_prepr z_pimpl z_prepr_wg

//Table 2, 3, and 4 main text
//eststo and esttab
foreach var of varlist(z_logRGTQ z_prepr z_pimpl){
	eststo clear
	local time i.t
	local robust cluster(ISO3166_1_num1)
	local controls z_logMeanGDP z_meanUDS
	local inter1 c.z_GiniWealth##c.z_logMeanGDP
	local inter2 c.z_GiniWealth##c.z_meanUDS
	local inter3 c.z_MeanGiniSWIID##c.z_logMeanGDP
	local inter4 c.z_MeanGiniSWIID##c.z_meanUDS
	/*model 1*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID `time', `robust'
	/*model 2*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID `controls' `time', `robust'
	/*model 3*/eststo: quie reg `var' z_MeanGiniSWIID `inter1' `inter2' `time', `robust'
	/*model 4*/eststo: quie reg `var' z_GiniWealth `inter3' `inter4' `time', `robust'
	local name = subinstr("`var'", "z_", "",.)
	local name2 = subinstr("`name'", "logR", "",.)
	local name3 = subinstr("`name2'", "GTQ", "2_GTQ",.) 
	local name4 = subinstr("`name3'", "prepr", "3_prepr",.)
	local name5 = subinstr("`name4'", "pimpl", "4_pimpl",.)
	esttab using Table_`name5'.rtf, order(z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS ///	
	c.z_GiniWealth#c.z_logMeanGDP c.z_GiniWealth#c.z_meanUDS c.z_MeanGiniSWIID#c.z_logMeanGDP c.z_MeanGiniSWIID#c.z_meanUDS) ///
	b(3) se(3) r2 varwidth(17) compress label nobaselevels nogaps ///
	noomitted interaction(" \f1 x \f0") /*onecell*/ title(Pooled cross-sectional models of `"`: var label `var''"') ///
	nonumbers mtitles("Model 1 Coefficient (SE)" "Model 2 Coefficient (SE)" "Model 3 Coefficient (SE)" "Model 4 Coefficient (SE)") ///
	nonotes addnotes("Note.---SE = Robust standard errors clustered at country level; GDP = Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS = Unified Democracy Score; GDP and Social Trust transformed using natural logarithm, all variables z-standardized." "* p < 0.05." "** p < 0.01." "*** p < 0.001.") ///
	fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs24" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
}

//see other changes by Jona in "changes made to the tables"

